package com.douya.pangu.application.sys.controller;

import com.douya.common.pojo.vo.CommonResponseVO;
import com.douya.pangu.application.sys.PanGuWebSocketHandler;
import com.douya.pangu.application.sys.pojo.dto.SocketDTO;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @Author hcy
 * @Date 2023/3/23
 */
@RestController
@RequestMapping(value = "/websocket", method = {RequestMethod.POST, RequestMethod.GET})/*GET请求开放用于测试，最好只允许POST请求*/
public class WebSocketController {

    @Resource
    PanGuWebSocketHandler springWebSocketHandler;

    /**
     * 指定发送
     */
    @ResponseBody
    @RequestMapping("/sendToUser")
    public CommonResponseVO<?> send(@RequestParam(value = "userId") Long userId) {
        String a = "pangu-socket/1";
        String b = "pangu-socket/2";
        for (int i = 0; i < 50; i++) {
            springWebSocketHandler.sendByUser(userId,
                    new SocketDTO()
                            .setContent("进度" + i + "%")
                            .setDestination(a)
            );
        }
        for (int i = 100; i < 200; i++) {
            springWebSocketHandler.sendByUser(userId,
                    new SocketDTO()
                            .setContent("进度" + i + "%")
                            .setDestination(b)
            );
        }
        return CommonResponseVO.success();
    }
}
